/*
 * Copyright (c) 1994-2017, Joyoung Stock CO.LTD.
 * The FWJR Project.
 * All Rights Reserved.
 */

package com.qdsg.ysg.doctor.ui.dialog;

import android.app.Dialog;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.qdsg.ysg.doctor.util.DpUtil;
import com.qdsg.ysg.doctor.util.SoftKeyboardUtil;
import com.qdsg.ysg.doctor.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.response.SickListResponse;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseDialogFragment;

import org.jetbrains.annotations.NotNull;


public class Item2Dialog extends BaseDialogFragment {
    private OnItemClickListener mListener;
    private Dialog mDialog;
    private EditText edt_search;
    private AddMedicineAdapter adapter;
    private List<SickListResponse.Sick> list = new ArrayList<>();

    public static Item2Dialog newInstance(int type) {
        Item2Dialog itemDialog = new Item2Dialog();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        itemDialog.setArguments(bundle);
        return itemDialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDialog = super.onCreateDialog(savedInstanceState);
        mDialog.setCancelable(true);
        Window window = mDialog.getWindow();
        if (window != null) {
            window.setGravity(Gravity.CENTER);
        }
        return mDialog;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View mView = inflater.inflate(R.layout.dialog_item2, container, false);
        mView.setMinimumWidth(mScreenWidth);
        assert getArguments() != null;
        int type = getArguments().getInt( "type" );
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        RecyclerView recyclerView = mView.findViewById( R.id.recyclerView );
        edt_search = mView.findViewById(R.id.edt_search);
        recyclerView.setLayoutManager(manager);
        adapter = new AddMedicineAdapter();
        recyclerView.setAdapter(adapter);

        edt_search.setOnEditorActionListener( (v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                // 按下完成按钮，这里和上面imeOptions对应
                getDicList(edt_search.getText().toString());
                SoftKeyboardUtil.hideSoftKeyboard( Objects.requireNonNull( getActivity() ) );
            }
            return false;
        } );
        edt_search.addTextChangedListener( new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                getDicList(s.toString().trim());
            }
        } );



        return mView;
    }

    @Override
    public void onViewCreated(@NotNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Objects.requireNonNull( getDialog() ).setCanceledOnTouchOutside(false);
    }

    public interface OnItemClickListener {
        void onItem(String s, String id);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }


    public boolean isShowing() {
        return mDialog != null && mDialog.isShowing();
    }


    class AddMedicineAdapter extends RecyclerView.Adapter<AddMedicineAdapter.Holder> {


        @NotNull
        @Override
        public AddMedicineAdapter.Holder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
            AddMedicineAdapter.Holder myHolder;
            myHolder = new AddMedicineAdapter.Holder(LayoutInflater.from(getActivity()).inflate(R.layout.item_textview, parent, false));
            return myHolder;
        }

        @Override
        public void onBindViewHolder(AddMedicineAdapter.Holder holder, final int position) {
            holder.itemView.setOnClickListener( v -> {
                    mListener.onItem(list.get(position).name,list.get(position).icdId);
                    dismiss();
            } );
            holder.tv_name.setText(list.get(position).name);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class Holder extends RecyclerView.ViewHolder {

            TextView tv_name;

            Holder(View itemView) {
                super(itemView);
                tv_name = itemView.findViewById(R.id.text);
            }
        }
    }

    private void getDicList(String content) {
        RestProxy.getInstance().getSickList(content,new Observer<SickListResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(SickListResponse sickListResponse) {
                    list.clear();
                    list.addAll(sickListResponse.data);
                    adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError(getActivity(), e);
            }

            @Override
            public void onComplete() {

            }
        });
    }

}
